package com.smartscreen.schedulemanage.service.impl;

import com.smartscreen.schedulemanage.dto.CreateCalendarEventRequest;
import com.smartscreen.schedulemanage.entity.CalendarEvent;
import com.smartscreen.schedulemanage.mapper.CalendarEventMapper;
import com.smartscreen.schedulemanage.service.CalendarEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Service
public class CalendarEventServiceImpl implements CalendarEventService {
    
    @Autowired
    private CalendarEventMapper calendarEventMapper;
    
    @Override
    public String createCalendarEvent(CreateCalendarEventRequest request) {
        String eventId = "evt_" + UUID.randomUUID().toString().substring(0, 8);
        
        CalendarEvent event = new CalendarEvent();
        event.setEventId(eventId);
        event.setTitle(request.getTitle());
        event.setType(request.getType());
        event.setStartTime(request.getStartTime());
        event.setEndTime(request.getEndTime());
        event.setDescription(request.getDescription());
        
        calendarEventMapper.insert(event);
        return eventId;
    }
    
    @Override
    public List<CalendarEvent> getCalendarEvents(LocalDate start, LocalDate end) {
        return calendarEventMapper.getEventsByDateRange(start, end);
    }
    
    @Override
    public void updateCalendarEvent(String eventId, CreateCalendarEventRequest request) {
        CalendarEvent event = new CalendarEvent();
        event.setEventId(eventId);
        event.setTitle(request.getTitle());
        event.setType(request.getType());
        event.setStartTime(request.getStartTime());
        event.setEndTime(request.getEndTime());
        event.setDescription(request.getDescription());
        
        calendarEventMapper.updateById(event);
    }
    
    @Override
    public void deleteCalendarEvent(String eventId) {
        calendarEventMapper.deleteById(eventId);
    }
}